<?php 
/**
 * @todo 查找好友 
 * @param $type=>age：同龄，local：城市，like：爱好 method=>get
 * @author 马超
 */
define('_VIEW_MODEL_','default');
session_start();
require_once("./init.php");
$uId      = isset($_SESSION['id'])?$_SESSION['id']:null; //用户id

if(empty($uId)){ //验证没有登录禁止访问
	errorPrompt('no_login','login.php');exit;
}

$type = (isset($type)&& in_array($type,array('age','local','like')))?$type:'age';

$userInfo = $db->getRow("select * from {$telper}user where id='$uId' and state='0'");

if(empty($userInfo)){ //验证用户信息是否存在
	errorPrompt('no_login','login.php');exit;
}
//分页
$page   = (isset($page))?intval($page):0; //当前页码
$perNum = 5; //每页显示记录数 

//进行分支查询不同的好友列表
if($type == 'age'){ //根据age查找
	$birth      = isset($userInfo['birthday'])?$userInfo['birthday']:0;
	$birthY     = date("Y",$birth);	
	$startTime  = mktime(0,0,0,01,01,$birthY);
	$endTime    = mktime(0,0,0,01,01,$birthY+1);
	$csql       = "select count(id) as count from {$telper}user where birthday >= '$startTime' 
					and birthday < '$endTime' and id <> '$uId' and state=0";
	$count      = $db->getRow($csql);
	$count      = $count['count'];
	$page       = min($page,ceil($count/$perNum));
	$page		= $page>0?$page:1;
	$start      = $perNum*($page - 1);
	
	$sql        = "select * from {$telper}user where birthday >= '$startTime' 
					and birthday < '$endTime' and id <> '$uId' and state=0 limit $start,$perNum";	
	$userList   = $db->getAll($sql);
	
}
	//整理数据
	foreach($userList as &$val){
		$val['sex'] = $val['sex']==0 ?'男':'女';
		$val['age'] = date("Y",time()) - date("Y",$val['birthday']);
	}
	//调用分页类
	$pageObj = new Page($count,$perNum,$smartyObj);	
	$smartyObj->assign('userlist',$userList);
	header("Content-type:text/vnd.wap.wml;charset=utf-8");
	$smartyObj->display('friendfind.tpl');
?>